<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 * @property Categoria $Categoria
 * @property Ciudade $Ciudade
 * @property Paise $Paise
 * @property Banco $Banco
 * @property Movimiento $Movimiento
 */
class User extends AppModel {
    
    public $virtualFields = array(
        'nombre_completo' => 'CONCAT(User.nombres," ",User.ap_paterno," ",User.ap_materno)'
    );
    
    public function beforeSave($options = array())
    {
        if(!empty($this->data['User']['password']))
        {
            $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        
        return true;
    }
    
    
    
    public $validate = array(
        'nombres' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Es obligatorio el nombre del Usario'
            )
        )
        ,'role' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Es obligatorio el tipo de perfil'
            )
        )
        ,'username' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Es obligatorio el ususario'
            )
            ,'limitDuplicates' => array(
                'rule' => array('limitDuplicates',1),
                'message' => 'Ya fue registrado el username'
            )
        )
        ,'ci' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Es obligatorio el ci'
            )
            ,'limitDuplicates' => array(
                'rule' => array('limitDuplicates',1),
                'message' => 'Ya fue registrado el C.I.'
            )
        )
        ,'rni' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Es obligatorio el RNI'
            )
            ,'limitDuplicates' => array(
                'rule' => array('limitDuplicates',1),
                'message' => 'Ya fue registrado el RNI'
            )
        )
        /*,'fecha_titulo' => array(
            'rule' => array('datetime','ymd'),
            'message' => 'No es una fecha valida!!!'
        )*/
        ,'categoria_id' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Es obligatorio la fecha del titulo'
            )
        )
    );
    
    public function limitDuplicates($check, $limit) {
        
            if(!empty($this->data['User']['id']))
            {
                $idp = $this->data['User']['id'];
                $check['User.id !='] = $idp;
            } 
            $drol=$this->data['User']['role'];
            $check['User.role']=$drol;
                
            $existingPromoCount = $this->find('count', array(
                'conditions' => $check,
                'recursive' => -1
            ));
            return $existingPromoCount < $limit;
        
    }
    

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Categoria' => array(
			'className' => 'Categoria',
			'foreignKey' => 'categoria_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Ciudade' => array(
			'className' => 'Ciudade',
			'foreignKey' => 'ciudade_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Paise' => array(
			'className' => 'Paise',
			'foreignKey' => 'paise_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Banco' => array(
			'className' => 'Banco',
			'foreignKey' => 'banco_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Movimiento' => array(
			'className' => 'Movimiento',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
